﻿Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class PictureBox
    Inherits System.Windows.Forms.PictureBox


    Private m_ProgressTextFont As Font = SystemFonts.DefaultFont
    <Browsable(True), DefaultValue(GetType(Font), "DefaultFont"), Description("进度文本字体"), Category("ajayumi.Properties")> _
    Public Property ProgressTextFont() As Font
        Get
            Return m_ProgressTextFont
        End Get
        Set(ByVal value As Font)
            If value IsNot Nothing Then
                m_ProgressTextFont = value
            Else
                Me.m_ProgressTextFont = SystemFonts.DefaultFont
            End If
            MyBase.Invalidate()
        End Set
    End Property


    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        Me.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
    End Sub

    Protected Overrides Sub OnLoadProgressChanged(ByVal e As System.ComponentModel.ProgressChangedEventArgs)
        MyBase.OnLoadProgressChanged(e)

        Dim _ProgressValue As String = String.Concat(CStr(e.ProgressPercentage), "%")
        Dim _FontSize As Size = TextRenderer.MeasureText(_ProgressValue, Me.ProgressTextFont)
        Dim _FontPoint As New Point((Me.Width - _FontSize.Width) / 2, (Me.Height - _FontSize.Height) / 2)
        Dim _Graphics As Graphics = Me.CreateGraphics()

        Try
            _Graphics.Clear(Me.BackColor)
#If DEBUG Then
            _Graphics.DrawLine(Pens.Black, CInt(Me.Width / 2), 0, CInt(Me.Width / 2), Me.Height)
            _Graphics.DrawLine(Pens.Black, 0, CInt(Me.Height / 2), Me.Width, CInt(Me.Height / 2))
            Dim _FontBrush As New SolidBrush(Color.FromArgb(100, 234, 234, 234))
            _Graphics.FillRectangle(_FontBrush, New Rectangle(_FontPoint, _FontSize))
#End If

            TextRenderer.DrawText(_Graphics, _ProgressValue, Me.ProgressTextFont, _FontPoint, Color.Red)
        Catch ex As Exception
            Throw ex
        Finally
        End Try
    End Sub
End Class


